clear
set mem 1g
set matsize 10000
set more off

use indfamcah85_1
gen year=85
save psid-80s, replace

local t "86 87 88 89"
foreach x of local t{
use indfamcah`x'_1
gen year=`x'
append using psid-80s
save psid-80s, replace
}

replace year=year+1900
gen id=hohin85 if year==1985
replace id=hohin86 if year==1986
replace id=hohin87 if year==1987
replace id=hohin88 if year==1988
replace id=hohin89 if year==1989

dmerge id year using psid-stateid
drop if _merge==2
drop _merge

gen birth=1 if births_1985>0 & births_1985~=. & year==1985
replace birth=1 if births_1986>0 & births_1986~=. & year==1986
replace birth=1 if births_1987>0 & births_1987~=. & year==1987
replace birth=1 if births_1988>0 & births_1988~=. & year==1988
replace birth=1 if births_1989>0 & births_1989~=. & year==1989
replace birth=0 if birth==.

gen nohs=1 if educ<12 & educ~=.
replace nohs=0 if nohs==.
gen hsdip=educ==12
gen somecoll=1 if educ>12 & educ<16 & educ~=.
replace somecoll=0 if somecoll==.
gen ba=1 if educ==16 | educ==17
replace ba=0 if ba==.
gen edmiss=1 if educ==. | educ==98
replace edmiss=0 if edmiss==.

gen age=age85 if year==1985
replace age=age86 if year==1986
replace age=age87 if year==1987
replace age=age88 if year==1988
replace age=age89 if year==1989
gen agesq=age*age

gen ownhome=housevalue_lag0~=0
replace ownhome=0 if housevalue_lag0==.
replace ownhome=0 if housevalue_lag0==999999

gen cpi=215.303/107.6 if year==1985
replace cpi=215.303/109.7 if year==1986
replace cpi=215.303/113.6 if year==1987
replace cpi=215.303/118.3 if year==1988
replace cpi=215.303/123.9 if year==1989

replace housevalue_lag0=0 if housevalue_lag0==9999998 | housevalue_lag0==9999999
replace housevalue_lag2=0 if housevalue_lag2==9999998 | housevalue_lag2==9999999
replace housevalue_lag4=0 if housevalue_lag4==9999998 | housevalue_lag4==9999999

gen rhomeval=cpi*housevalue_lag0/10000

gen rhvaldiff_2=(housevalue_lag0-housevalue_lag2)*cpi
replace rhvaldiff_2=0 if ownhome==0

gen rhvaldiff_4=(housevalue_lag0-housevalue_lag4)*cpi
replace rhvaldiff_4=0 if ownhome==0

replace rhvaldiff_2=rhvaldiff_2/10000
replace rhvaldiff_4=rhvaldiff_4/10000

gen rtot_fam_y_lag0=(cpi*tot_fam_y_lag0)/10000
gen rtot_fam_y_lag2=(cpi*tot_fam_y_lag2)/10000
gen rtot_fam_y_lag4=(cpi*tot_fam_y_lag4)/10000

gen decade_80s=1

gen rhvaldiff_2_80s=rhvaldiff_2*decade_80s
gen rhvaldiff_4_80s=rhvaldiff_4*decade_80s

drop if stfips==.
drop if stfips==.
gen asample=1 if rhvaldiff_2~=. & rhvaldiff_4~=. 
sort hohin85
gen yr=year
save psid-80s, replace

use psid-geocode
keep YEAR FAMID FIPSTATE MSA00 TRACT00
ren FAMID hohin85
ren YEAR year
sort hohin85
keep if year==1985
merge hohin85 using psid-80s
tab _merge if yr==1985
drop if _merge==1
gen geomerge=_merge if yr==1985
drop _merge
gen fips_state=FIPSTATE if yr==1985
gen msa=MSA00 if yr==1985
gen centract=TRACT00 if yr==1985
drop FIPSTATE FIPSTATE TRACT00
sort hohin86
save psid-80s, replace

use psid-geocode
keep YEAR FAMID FIPSTATE MSA00 TRACT00
ren FAMID hohin86
ren YEAR year
sort hohin86
keep if year==1986
merge hohin86 using psid-80s
tab _merge if yr==1986
drop if _merge==1
replace geomerge=_merge if yr==1986
drop _merge
replace fips_state=FIPSTATE if yr==1986
replace msa=MSA00 if yr==1986
replace centract=TRACT00 if yr==1986
drop FIPSTATE FIPSTATE TRACT00
sort hohin87
save psid-80s, replace

use psid-geocode
keep YEAR FAMID FIPSTATE MSA00 TRACT00
ren FAMID hohin87
ren YEAR year
sort hohin87
keep if year==1987
merge hohin87 using psid-80s
tab _merge if yr==1987
drop if _merge==1
replace geomerge=_merge if yr==1987
drop _merge
replace fips_state=FIPSTATE if yr==1987
replace msa=MSA00 if yr==1987
replace centract=TRACT00 if yr==1987
drop FIPSTATE FIPSTATE TRACT00
sort hohin88
save psid-80s, replace

use psid-geocode
keep YEAR FAMID FIPSTATE MSA00 TRACT00
ren FAMID hohin88
ren YEAR year
sort hohin88
keep if year==1988
merge hohin88 using psid-80s
tab _merge if yr==1988
drop if _merge==1
replace geomerge=_merge if yr==1988
drop _merge
replace fips_state=FIPSTATE if yr==1988
replace msa=MSA00 if yr==1988
replace centract=TRACT00 if yr==1988
drop FIPSTATE FIPSTATE TRACT00
sort hohin89
save psid-80s, replace

use psid-geocode
keep YEAR FAMID FIPSTATE MSA00 TRACT00
ren FAMID hohin89
ren YEAR year
sort hohin89
keep if year==1989
merge hohin89 using psid-80s
tab _merge if yr==1989
drop if _merge==1
replace geomerge=_merge if yr==1989
drop _merge
replace fips_state=FIPSTATE if yr==1989
replace msa=MSA00 if yr==1989
replace centract=TRACT00 if yr==1989
drop FIPSTATE FIPSTATE TRACT00
save psid-80s, replace

drop year
ren yr year
drop geomerge
save psid-80s, replace


